/*
 * Copyright (C) 2011 xjuraj
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package sk.lieskove.jianghongtiao.websearch.document.preprocessing.actions;

import org.apache.log4j.Logger;
import sk.lieskove.jianghongtiao.common.utils.FileUtils;
import sk.lieskove.jianghongtiao.common.utils.text.LanguageDetection;
import sk.lieskove.jianghongtiao.websearch.document.preprocessing.SupportedLanguages;
import sk.lieskove.jianghongtiao.websearch.persistence.WebSearchResponse;

import java.io.IOException;
import java.io.Serializable;

/**
 * 
 * @author xjuraj e-mail: jjurco.sk_gmail.com
 */
public class LangRecognition implements Serializable, PreprocessAction {

    private transient Logger log = Logger.getLogger(LangRecognition.class);

    @Override
    public WebSearchResponse preprocess(WebSearchResponse response) {
        log.debug("WebSearchResponse preprocess(WebSearchResponse response)");
        try {
            LanguageDetection languageDetection = LanguageDetection.getInstance();
            
            response.setLanguageName(SupportedLanguages.Afrikaans.fromIso369_1(
                    languageDetection.detectLang(FileUtils.read(response.getFile()))));
        } catch (IOException ex) {
            log.error("IO exception thrown while loading file for detection: "
                    + response.getFile().getAbsolutePath());
        }
        return response;
    }
}
